<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5" :xs="24">
        <div class="head-container">
          <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="设备名称" prop="name">
              <el-input
                  v-model="queryParams.name"
                  placeholder="请输入设备名称"
                  clearable
                  @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-table
              ref="devicelistleftforRef"
              v-loading="loading"
              :data="deviceList"
              highlight-current-row
              @current-change="handleCurrentChange"
              :default-sort="defaultSort"
              @sort-change="handleSortChange"
          >
            <el-table-column label="设备名称" align="center" prop="name">
            </el-table-column>
            <el-table-column label="设备编号" align="center" prop="number"/>
            <el-table-column label="设备状态" align="center" prop="status"
                             sortable="custom"
                             :sort-orders="['descending', 'ascending']">
              <template #default="scope">
                <dict-tag :options="dicts.device_status" :value="scope.row.status"/>
              </template>
            </el-table-column>
          </el-table>
          <pagination
              class="pagination-container"
              :layout="'prev, pager, next'"
              :autoScroll="false"
              v-show="total > 0"
              :total="total"
              :small="true"
              :background="false"
              v-model:page="queryParams.pageNum"
              v-model:limit="queryParams.pageSize"
              @pagination="getList"
          />
        </div>
      </el-col>
      <el-col :span="19" :xs="24">
        <span/>
        <el-divider content-position="left">设备名称:{{ deviceName }}</el-divider>
        <div class="head-container">
          <el-table
              :data="tableData"
              highlight-current-row
          >
            <el-table-column prop="dataName" label="数据点" width="180"/>
            <el-table-column prop="dataNameType" label="数据点类型" width="180"/>
            <el-table-column prop="deviceNum" label="设备编号" width="180"/>
            <el-table-column prop="deviceRealName" label="从机名称" width="180"/>
            <el-table-column prop="dateTime" label="更新时间">
              <template #default="scope">
                <span>{{ parseTime(scope.row.dateTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="当前值" align="center" prop="flag">
              <template #default="scope">
                <dict-tag :options="dicts.device_collection_status" :value="scope.row.flag"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-tooltip content="主动采集" placement="top">
                  <el-button link type="primary" icon="UploadFilled" @click="handleCollection(scope.row)"></el-button>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
  </div>
</template>


<script>
import {getCurrentInstance} from "vue";
import {doSendCommand, getCurrentUserDevicesAndDatas} from "@/api/device";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "DeviceList",
  data() {
    return {
      // 是否显示弹出层
      show: false,

      tableData: [],
      //字典
      dicts: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      deviceList: [],
      // 设备名称
      deviceName: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        number: null,
        status: null,
        orderByColumn: undefined,
        isAsc: undefined
      },
      //排序
      defaultSort: {prop: 'status', order: 'descending'},
    }
  },
  created() {
    this.getList();
    const {proxy} = getCurrentInstance();
    this.dicts = proxy.useDict("device_collection_status", "device_status");
  },
  methods: {
    parseTime,
    tableRowClassName(row, rowIndex,) {
      if (rowIndex === 1) {
        return 'warning-row'
      } else if (rowIndex === 3) {
        return 'success-row'
      }
      return ''
    },
    handleCurrentChange(val) {

      if (val === undefined || val === '' || val === null) {
        if (this.deviceList.length > 0) {
          this.deviceName = this.deviceList[0].name;
        }
      } else {
        this.deviceName = val?.name;
        this.tableData = val.deviceDataList;
      }
    },

    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      getCurrentUserDevicesAndDatas(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.deviceName = response.rows[0]?.name;
        this.tableData = response.rows[0]?.deviceDataList;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      queryParams.value.pageNum = 1;
      proxy.$refs["devicelistleftforRef"].sort(this.defaultSort.value.prop, this.defaultSort.value.order);
      // this.handleQuery();
    },
    /**
     * 主动采集
     */
    handleCollection(row) {

      doSendCommand(row.deviceNum).then(response => {
        console.log("response==>", response)
        if (response.code === 200) {
          this.$message.success("下达成功");
        } else {
          this.$message.error("下达失败");
        }
      })
      console.log("row==>", row)
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      debugger
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    }

  }
}

</script>

<style>
.pagination-container {
  right: 0;
  position: relative;
}
</style>
